package com.bonana.core.services;

import com.bonana.core.dao.utils.CityDAO;
import com.bonana.core.entity.utils.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Justin on 2014-12-15.
 */
@Service
public class CityService {
    @Autowired
    private CityDAO cityDAO;

    public List<City> queryByParentId(int parentId) {
        return cityDAO.queryByParentId(parentId);
    }

    public List<City> getCitiesTree(int level) {
        return getSubList(0 , level);
    }

    public String queryNameById(int id) {
        return cityDAO.queryNameById(id);
    }

    public List<City> getSubList(int parentId , int level) {
        List<City> root = this.queryByParentId(parentId);
        List<City> result = new ArrayList<City>();
        for(City city : root) {
            if(city.getLevel() < 3 && city.getLevel() < level) {
                city.setSubList(getSubList(city.getId() , level));
            }
            result.add(city);
        }
        return result;
    }
}
